<template>
	<section>
		<!--高级查询-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="advancedQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="菜单名" sortable>
			</el-table-column>
      <el-table-column prop="url" label="URL" sortable>
      </el-table-column>
      <el-table-column prop="icon" label="icon" sortable>
      </el-table-column>
      <el-table-column prop="parent.name" label="父菜单" sortable>
      </el-table-column>
			<el-table-column label="操作">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--批量删除-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger"
                 @click="batchDelete"
                 :disabled="this.sels.length===0"
                 style="float: left;margin: 20px">
        批量删除</el-button>
      <!--分页条-->
      <el-pagination
          style="float: right;margin: 20px"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="query.currentPage"
          :page-sizes="pageSizes"
          :page-size="query.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.total">
      </el-pagination>
		</el-col>



		<!--保存界面-->
    <!--
       pros 绑定校验规则名称
       :visible.sync  控制dialog是否显示
       :rules  绑定表单校验规则
       ref="saveForm"  给当前表单取一个别名
    -->
		<el-dialog title="保存"
               :visible.sync="saveFormVisible"
               :close-on-click-modal="false">
			<el-form
          :model="saveForm"
          label-width="80px"
          :rules="saveFormRules"
          ref="saveForm">
				<el-form-item label="菜单名" prop="name">
					<el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>

				<el-form-item label="URL" >
					<el-input  v-model="saveForm.url"></el-input>
				</el-form-item>
        <el-form-item label="icon">
          <el-input  v-model="saveForm.icon"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单">
          <el-select v-model="saveForm.parentId" clearable value-key="id" placeholder="请选择">
            <!--下拉选项
              :key=""  //唯一标识
              :label 选择之后展示到选择框中的值
              :value 选中之后绑定给模型层的值  如果要绑定对象给模型层 有一个大坑
                 必须要写  value-key="id"
            -->
            <el-option
                v-for="item in menus"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
        <!--@click.native vue在自定义组件中使用原生事件要加上native-->
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">确认</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>

	export default {
		data() {
			return {
			  //列表显示信息
        pageInfo:{
          rows: [],
          total: 0,
        },
        //分页选择数量
        pageSizes:[5, 10, 20, 50],
        //高级查询对象
        query:{
				  //当前页,默认第一页
          currentPage:1,
          //页面显示数据大小,默认显示5条
          pageSize:5,
          //高级查询关键字
          keyword:""
        },
        //列表加载框
				listLoading: false,
				sels: [],//列表选中列

        saveFormVisible: false,//保存界面是否显示
        saveLoading: false,//保存加载框
        //保存校验规则
        saveFormRules: {
					name: [
						{ required: true, message: '请输入名称', trigger: 'blur' },
					]
				},
				//保存界面数据
        saveForm:{
          id:null,
          name: '',
          url:'',
          icon : '',
          parentId : null
        },
        menus:[],
			}
		},
		methods: {
		  getAllParentMenus(){
        this.$http.post("/menu/getAllParentMenus")
            .then(result => {
              result = result.data;
              this.menus = result.obj;
            })
            .catch(result => {
              this.$message({
                message: '网络错误!',
                type: 'error'
              });
            })
      },
      handleCurrentChange(val) {
        //改变当前显示页数
        this.query.currentPage = val;
        this.getMenus();
      },
      handleSizeChange(val) {
        //改变每页显示条数
        this.query.pageSize = val;
        this.getMenus();
      },
      //获取菜单列表
      getMenus() {
        this.listLoading = false;
        this.$http.post("/menu", this.query)
            .then(result => {
              result = result.data;
              console.log(result.obj.total);
              console.log(result.obj.rows);
              this.pageInfo = result.obj;
            })
            .catch(result => {
              this.$message({
                message: '网络错误!',
                type: 'error'
              });
            })
      },
      //高级查询
      advancedQuery() {
        //初始化页面
        this.query.currentPage = 1;
        //加载数据到页面
        this.getMenus();
      },
      //删除
      handleDel: function (index, row) {
        this.$confirm('数据将无法恢复!', '警告', {
          type: 'warning'
        }).then(() => {
          //弹出加载窗口
          this.listLoading = true;
          this.$http.delete("/menu/" + row.id)
              .then(result => {
                //操作成功进入then,关闭加载窗口
                this.listLoading = false;
                result = result.data;
                if (result.success) {
                  this.$message({
                    message: '删除成功!',
                    type: 'success'
                  });
                  //删除完成后刷新页面
                  this.advancedQuery();
                } else {
                  this.$message({
                    message: '删除失败!',
                    type: 'error'
                  });
                }
              })
              .catch(result => {
                this.$message({
                  message: '网络错误!',
                  type: 'error'
                });
              })
        }).catch(() => {
          this.$message({
            message: '网络错误!',
            type: 'error'
          });
        });
      },
      //显示编辑界面
      handleEdit: function (index, row) {
        //弹出界面
        this.saveFormVisible = true;
        this.saveForm = Object.assign({}, row);
        this.getAllParentMenus();
      },
      //显示新增界面
      handleAdd: function () {
        //弹出界面
        this.saveFormVisible = true;
        //初始化
        this.saveForm = {
          id:null,
              name: '',
              url:'',
              icon : '',
              parentId : null
        }
        this.getAllParentMenus();

      },
      //保存按钮
      saveSubmit: function () {
        this.$refs.saveForm.validate((valid) => {
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.saveLoading = true;
              //NProgress.start();
              let para = Object.assign({}, this.saveForm);

              /*console.log("保存菜单数据")
              console.log(para)
              return */
              this.$http.put("/menu/saveMenu", para)
                  .then(result => {
                    result = result.data;
                    if (result.success) {
                      this.advancedQuery();

                      this.saveFormVisible = false;
                      this.$message({message: '保存成功', type: 'success'});
                    } else {
                      this.$message({message: result.message, type: 'error'});
                    }
                    this.saveLoading = false;

                  })
            });
          }
        });
      },
      selsChange: function (sels) {
        this.sels = sels;
      },
      //批量删除
      batchDelete: function () {
        //从sels中获取所有被选中的id
        var ids = this.sels.map(item => item.id);
        this.$confirm('数据将无法恢复!', '警告', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          //除get请求其他请求传参应该加上requestBody否侧无数据
          this.$http.patch("/menu", ids)
              .then(result => {
                //无论成功与否都要关闭加载窗口
                this.listLoading = false;
                //后端返回的数据会被包在data中
                result = result.data;
                if (result.success) {
                  this.$message({
                    message: '删除成功!',
                    type: 'success'
                  });
                  //刷新页面
                  this.advancedQuery();
                } else {
                  this.$message({
                    message: '删除失败!',
                    type: 'error'
                  });
                }
              })
              .catch(result => {
                this.$message({
                  message: '网络错误!',
                  type: 'error'
                });
              })
        }).catch(() => {
          this.$message({
            message: '网络错误!',
            type: 'error'
          });
        });
      }
    },
		mounted() {
			this.getMenus();
		}
	}

</script>

<style scoped>

</style>